// *********************************************************
//      Copyright (c) Kosta Gaitanis
//      All rights reserved
//
//      k.gaitanis@gmail.com
//
// *********************************************************

#include "LightsListWidget.h"

const int lightPointerRole = Qt::UserRole;

LightsListWidget::LightsListWidget(QWidget *parent) :
    CollapsibleWidget(parent),
    m_combo(new QComboBox()),
    m_ambient(new ColorPickerWidget("Ambient")),
    m_diffuse(new ColorPickerWidget("Diffuse")),
    m_specular(new ColorPickerWidget("Specular")),
    m_checkBox(new QCheckBox("On"))
{    
    connect(m_combo, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentLightChanged(int)));
    connect(m_ambient, SIGNAL(colorChanged(QColor)), this, SLOT(onAmbientChanged(QColor)));
    connect(m_diffuse, SIGNAL(colorChanged(QColor)), this, SLOT(onDiffuseChanged(QColor)));
    connect(m_specular, SIGNAL(colorChanged(QColor)), this, SLOT(onSpecularChanged(QColor)));
    connect(m_checkBox, SIGNAL(toggled(bool)), this, SLOT(onOnOffChanged(bool)));

    QWidget *w = new QWidget();
    QVBoxLayout *layout = new QVBoxLayout(w);
    layout->addWidget(m_combo);
    layout->addWidget(m_ambient);
    layout->addWidget(m_diffuse);
    layout->addWidget(m_specular);
    layout->addWidget(m_checkBox, 1, Qt::AlignRight);
    w->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);

    setTitle("Lights");
    setWidget(w);
}

void LightsListWidget::addLight(Light *light)
{
    m_combo->addItem(light->getName(), QVariant::fromValue<Light*>(light));
}

void LightsListWidget::onCurrentLightChanged(int )
{
    Light *light = getCurrentLight();
    if (light) {
        setAmbient(light->getAmbient());
        setDiffuse(light->getDiffuse());
        setSpecular(light->getSpecular());
        setOn(light->isOn());
    }
}

void LightsListWidget::onAmbientChanged(const QColor &color)
{
    Light* light = getCurrentLight();
    if (light) {
        light->setAmbient(color);
        emit lightModified();
    }
}

void LightsListWidget::onDiffuseChanged(const QColor &color)
{
    Light* light = getCurrentLight();
    if (light) {
        light->setDiffuse(color);
        emit lightModified();
    }
}

void LightsListWidget::onSpecularChanged(const QColor &color)
{
    Light* light = getCurrentLight();
    if (light) {
        light->setSpecular(color);
        emit lightModified();
    }
}

void LightsListWidget::onOnOffChanged(bool on)
{
    Light* light = getCurrentLight();
    if (light) {
        light->setOn(on);
        emit lightModified();
    }
}

Light *LightsListWidget::getCurrentLight() const
{
    return m_combo->itemData(m_combo->currentIndex()).value<Light*>();
}

void LightsListWidget::setAmbient(const QColor &color)
{
    m_ambient->setColor(color);
}

void LightsListWidget::setDiffuse(const QColor &color)
{
    m_diffuse->setColor(color);
}

void LightsListWidget::setSpecular(const QColor &color)
{
    m_specular->setColor(color);
}

void LightsListWidget::setOn(bool on)
{
    m_checkBox->setChecked(on);
}

QColor LightsListWidget::getAmbient() const
{
    return m_ambient->getColor();
}

QColor LightsListWidget::getDiffuse() const
{
    return m_diffuse->getColor();
}

QColor LightsListWidget::getSpecular() const
{
    return m_specular->getColor();
}

